var form = {
    isoperate: false,
    go: function (url) {
        window.location = url;
    },
    ajax: function (url, data, callback) {
        if (form.isoperate) {
            Page.$message({
                message: '正在操作中...',
                type: 'warning'
            });
            return;
        }
        form.isoperate = true;
        miiteen.ajax.post(url, data, callback, form.errorcallback);
    },
    successcallback: function (response) {
        var result = response.data;
        if (result.state > 0) {
            form.msg.success(result.message);
            if (result.code == "redirect") {
                //跳转页面
                window.parent.Home.closeFormView();
                window.parent.Home.openListView(result.data);
            }
        } else {
            switch (result.state) {
                case -400:
                    form.msg.error("参数错误");
                    break;
                case -401:
                    form.msg.error("无访问权限");
                    break;
                case -404:
                    form.msg.error("请求的页面不存在");
                    break;
                case -500:
                    form.msg.error("服务器错误");
                    break;
                case -601:
                    form.msg.warnbox("长时间无操作，请重新登陆", function (action) {
                        if (action == "confirm") {
                            window.parent.location.href = "/login/index";
                        }
                    });
                    break;
            }
        }
        form.isoperate = false;
    },
    errorcallback: function () {
        form.isoperate = false;
    }
}

/**
 * 页面提示/弹窗
 * @type {{success: form.msg.success, warnbox: form.msg.warnbox, error: form.msg.error, confirm: form.msg.confirm, dialog: form.msg.dialog}}
 */
form.msg = {
    success: function (msg, callback) {
        window.parent.mtMsg.success(msg, callback);
    },
    warnbox: function (msg, callback) {
        window.parent.mtMsg.warnbox(msg, callback);
    },
    error: function (msg, callback) {
        window.parent.mtMsg.error(msg, callback);
    },
    confirm: function (msg, callback) {
        window.parent.mtMsg.confirm(msg, callback);
    },
    dialog: function (title, content) {
        window.parent.mtMsg.dialog(title, content);
    }
}

/**
 * 列表界面
 * @type {{init: form.list.init, new: form.list.new, del: form.list.del, search: form.list.search, method: {getgridheight: form.list.method.getgridheight}}}
 */
form.list = {
    init: function (page) {
        page.grid.height = this.method.getgridheight();
    },
    add: function (page) {
        window.parent.Home.openFormView(page.controller + "/edit");
    },
    del: function (page, param) {
        if (page.gridselect.length < 1) {
            form.msg.warnbox("请选择要删除的数据");
            return;
        }
        var ids = "";
        for (var i = 0; i < page.gridselect.length; i++) {
            ids += (ids == "" ? "" : ",") + page.gridselect[i].id;
        }

        miiteen.ajax.postconfirm(page.controller + "/del?ids=" + ids, param ? param : {}, "确认要删除选择的数据吗？", function (response) {
            form.successcallback(response);
            //删除后刷新
            form.list.search(page, page.controller + "/search");
        }, form.errorcallback)
    },
    delSingle: function (page, id) {
        miiteen.ajax.postconfirm(page.controller + "/del?ids=" + id, {}, "确认要删除选择的数据吗？", function (response) {
            form.successcallback(response);
            //删除后刷新
            form.list.search(page, page.controller + "/search");
        }, form.errorcallback)
    },
    edit: function (page, id) {
        window.parent.Home.openFormView(page.controller + "/edit?id=" + id);
    },
    view: function (page, id) {
        window.parent.Home.openFormView(page.controller + "/view?id=" + id);
    },
    search: function (page, url) {
        var con = {searchcon: page.form, pagecon: {pageSize: page.grid.pageSize, pageNum: page.grid.curPage}};
        miiteen.ajax.post(url, con, function (response) {
            var result = response.data;
            if (result.state > 0) {
                page.grid.data = result.data;
                page.grid.total = Number(result.message);
            } else {
                page.grid.data = [];
            }
        }, function () {

        })
    },
    method: {
        getgridheight: function () {
            var mainheight = document.getElementsByClassName("mainpage")[0].clientHeight;
            var topmenus = document.getElementsByClassName("mt-toolbar");
            var forms = document.getElementsByClassName("mt-search-form");

            var gridheight = mainheight;
            for (var i = 0; i < topmenus.length; i++) {
                gridheight -= topmenus[i].clientHeight;
            }
            for (var i = 0; i < forms.length; i++) {
                gridheight -= forms[i].clientHeight;
            }
            gridheight -= (16 + 60);
            if (gridheight < 300) {
                return 300;
            } else {
                return gridheight;
            }
        }
    }
}

//gird相关
form.grid = {
    height: 300,
    data: [],
    total: 0,
    pagelayout: "sizes, prev, pager, next",
    pageSizes: [10, 20, 50, 100],
    pageSize: 10,
    curPage: 1
}

/**
 * 编辑页面
 * @type {{back: form.edit.back, save: form.edit.save}}
 */
form.edit = {
    labelwidth: "100px",
    labelposition: "right",
    back: function (page) {
        //返回
        window.parent.Home.closeFormView();
    },
    save: function (page, formName, formdata, url, callback) {
        page.$refs[formName].$children[0].validate(valid => {
            if (valid) {
                form.ajax(url, formdata, callback ? callback : form.successcallback);
            } else {
                return false
            }
        });
    }
}

/**
 * 查看页面
 * @type {{}}
 */
form.view = {
    labelwidth: "100px",
    labelposition: "right",
    back: function (page) {
        //返回
        window.parent.Home.closeFormView();
    }
}

form.method = {}